package com.vshop.controller.externalInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.comentity.PartnerPackage;
import com.vshop.entity.external.listpackage.request.RequestServicePlanList;
import com.vshop.entity.external.listpackage.request.RequestServicePlanListData;
import com.vshop.entity.external.listpackage.response.ResponseServicePlanList;
import com.vshop.entity.external.listpackage.response.ResponseServicePlanListData;
import com.vshop.entity.external.listpackage.response.ResponseServicePlanListTradeData;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.service.PartnerPackageService;
import com.vshop.service.SequenceService;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 专门用于外部接口来：获取套餐列表
 *
 * @author lm
 * @date 2019/09/03
 */
@RestController
@Api(description = "获取套餐列表")
@Slf4j
public class GetServicePlanListController {
    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private PartnerPackageService partnerPackageService;

    @CrossOrigin
    @PostMapping(value = "servicePlanList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "获取套餐列表", response = ResponseServicePlanList.class)
    public String servicePlanList(@ApiParam @RequestBody RequestServicePlanList request) {
        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();

        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());

        //3,定义要回复的内容
        String jsonString;

        ExceptionRequest utils = new ExceptionRequest();
        String requestStr = JSON.toJSONString(request);
        if (requestStr == null) {
            log.info("请求报文不合法:requestStr为空");
            jsonString = utils.dataWeiKong(connSeqNo, curtime);
            return jsonString;
        } else {
            log.info("前端请求报文是：" + requestStr);
            RequestServicePlanList requestPackage = JSONArray.parseObject(requestStr, RequestServicePlanList.class);
            String requestSign = requestPackage.getSign();
            if (requestPackage.getSign() == null) {
                log.info("请求报文不合法:sign为空");
                jsonString = utils.dataWeiKong(connSeqNo, curtime);
                return jsonString;
            } else {
                log.info("请求的签名是：" + requestSign);
                RequestServicePlanListData requestData = requestPackage.getData();
                if (requestData == null) {
                    log.info("请求报文不合法:data为空");
                    jsonString = utils.dataWeiKong(connSeqNo, curtime);
                    return jsonString;
                } else {

                    String ConnSeqNo = requestData.getConnSeqNo();
                    String partnerCode = requestData.getPartnerCode();
                    String tradeTime = requestData.getTradeTime();
                    String tradeType = requestData.getTradeType();
                    String version = requestData.getVersion();
                    String lang = requestData.getLang();

                    if (StringUtils.isBlank(ConnSeqNo) || StringUtils.isBlank(partnerCode) || StringUtils.isBlank(tradeTime) || StringUtils.isBlank(tradeType) || StringUtils.isBlank(version) || StringUtils.isBlank(lang)) {
                        log.info("请求报文不合法:data参数不合法");
                        jsonString = utils.dataWeiKong(connSeqNo, curtime);
                        return jsonString;
                    } else {

                        String str = JSON.toJSONString(requestData);
                        String sign = utils.calculateSignature(str);
                        log.info("计算的签名是：" + sign);

                        if (!requestSign.equalsIgnoreCase(sign)) {

                            jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "签名有误，请确认签名！");
                            return jsonString;

                        } else {

                            List<PartnerPackage> planList = partnerPackageService.getServicePlanList(partnerCode);

                            if (planList != null && planList.size() > 1) {

                                List<ResponseServicePlanListTradeData> tradeDataList = new ArrayList<ResponseServicePlanListTradeData>();

                                ResponseServicePlanListTradeData tradeData = null;

                                for (PartnerPackage p : planList) {

                                    tradeData = new ResponseServicePlanListTradeData();

                                    tradeData.setPackageCode(p.getPackageCode());
                                    tradeData.setPackageName(p.getPackageName());
                                    tradeData.setPackageType(p.getPackageType());
                                    if (StringUtils.isNotBlank(p.getPackageFlow())) {
                                        tradeData.setPackageFlow(p.getPackageFlow());
                                    } else {
                                        tradeData.setPackageFlow("");
                                    }
                                    if (StringUtils.isNotBlank(p.getBuyType())) {
                                        tradeData.setBuyType(p.getBuyType());
                                    } else {
                                        tradeData.setBuyType("");
                                    }
                                    if (StringUtils.isNotBlank(p.getPriceType())) {
                                        tradeData.setPriceType(p.getPriceType());
                                    } else {
                                        tradeData.setPriceType("");
                                    }
                                    tradeData.setMinOrderPeriod(p.getMinOrderPeriod());
                                    tradeData.setMaxOrderPeriod(p.getMaxOrderPeriod());
                                    tradeData.setCoverMcc(p.getCoverMcc());
                                    tradeData.setCoverCountry(p.getCoverCountrys());
                                    tradeData.setContinentName(p.getContinentName());
                                    tradeData.setPackageStatus(p.getPackageStatus());
                                    tradeData.setSalePriceCNY(p.getOriginalPriceCNY());
                                    tradeData.setSalePriceUSD(p.getOriginalPriceUSD());
                                    tradeDataList.add(tradeData);
                                }

                                ResponseServicePlanListData data = new ResponseServicePlanListData();
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeType(tradeType);
                                data.setTradeTime(curtime);
                                data.setRetCode("0000");
                                data.setRetMsg("成功");
                                data.setTradeData(tradeDataList);

                                ResponseServicePlanList response = new ResponseServicePlanList();
                                response.setData(data);
                                response.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                // 返回给调用者的报文
                                jsonString = JSON.toJSONString(response);
                                log.info("响应报文：" + jsonString);
                                return jsonString;

                            } else {
                                log.info("未查询到渠道存在可售卖的套餐：");
                                ResponseServicePlanListData data = new ResponseServicePlanListData();
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeType(tradeType);
                                data.setTradeTime(curtime);
                                data.setRetCode("0200");
                                data.setRetMsg("未获取到可售卖的套餐列表");

                                ResponseServicePlanList response = new ResponseServicePlanList();
                                response.setData(data);
                                response.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                // 返回给调用者的报文
                                jsonString = JSON.toJSONString(response);
                                log.info("响应报文：" + jsonString);
                                return jsonString;

                            }


                        }
                    }
                }

            }
        }
    }
}
